package com.hf.eagle.web.struts.admin.manager;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.validator.GenericValidator;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ebiz.ssi.web.struts.bean.UploadFile;
import com.hf.eagle.domain.SiteLink;
import com.hf.eagle.domain.UserInfo;
import com.hf.eagle.web.base.BaseAction;
import com.hf.eagle.web.base.Keys;
import com.hf.eagle.web.base.Pager;
import com.hf.eagle.web.base.SysConstant;

/**
 * @author Xu,XiaoYuan
 * @version 2011.10.31 23:53
 */
public class AdvertisementAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.list(mapping, form, request, response);
	}

	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		DynaBean dynaBean = (DynaBean) form;
		Pager pager = (Pager) dynaBean.get("pager");
		String mod_id = (String) dynaBean.get("mod_id");
		String title_like = (String) dynaBean.get("title_like");

		SiteLink siteLink = new SiteLink();
		siteLink.setIs_del(0);
		siteLink.setMod_id(Long.valueOf(mod_id));
		super.copyProperties(siteLink, form);
		siteLink.getMap().put("title_like", title_like);

		Long recordCount = super.getFacade().getSiteLinkService().getSiteLinkCount(siteLink);
		pager.init(recordCount, pager.getPageSize(), pager.getRequestPage());
		siteLink.getRow().setFirst(pager.getFirstRow());
		siteLink.getRow().setCount(pager.getLastRow());

		List<SiteLink> list = super.getFacade().getSiteLinkService().getSiteLinkPaginatedList(siteLink);
		request.setAttribute("entityList", list);

		super.copyProperties(form, siteLink);

		return mapping.findForward("list");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		return mapping.findForward("input");
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		DynaBean dynaBean = (DynaBean) form;
		String mod_id = (String) dynaBean.get("mod_id");
		String id = (String) dynaBean.get("id");

		SiteLink siteLink = new SiteLink();
		siteLink.setId(Long.valueOf(id));
		siteLink.setMod_id(Long.valueOf(mod_id));
		siteLink.setIs_del(0);
		siteLink = super.getFacade().getSiteLinkService().getSiteLink(siteLink);
		if (null != siteLink) {
			siteLink.setQueryString(super.serialize(request, "id", "method", "mod_id"));
		}
		super.copyProperties(form, siteLink);

		return mapping.findForward("input");
	}

	public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		DynaBean dynaBean = (DynaBean) form;
		String id = (String) dynaBean.get("id");

		SiteLink siteLink = new SiteLink();
		siteLink.setId(Long.valueOf(id));
		siteLink = super.getFacade().getSiteLinkService().getSiteLink(siteLink);

		super.copyProperties(form, siteLink);

		return mapping.findForward("view");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		String mod_id = (String) dynaBean.get("mod_id");

		SiteLink siteLink = new SiteLink();
		super.copyProperties(siteLink, form);
		siteLink.setIs_del(0);

		HttpSession session = request.getSession();
		UserInfo userInfo = (UserInfo) session.getAttribute(Keys.ADMIN_USER);

		List<UploadFile> uploadFileList = this.uploadFile(form, "", true, SysConstant.ADVERTISE_LOG_SIZE);
		if (null != uploadFileList && uploadFileList.size() > 0) {
			for (UploadFile uploadFile : uploadFileList) {
				if (uploadFile.getFileSize() > 0) {
					siteLink.setLogo_url(uploadFile.getFileSavePath());
				}
			}
		}

		if (null == siteLink.getId()) {
			siteLink.setAdd_user_id(userInfo.getUser_id());
			siteLink.setAdd_user_name(userInfo.getUser_name());
			super.getFacade().getSiteLinkService().createSiteLink(siteLink);
			saveMessage(request, "entity.inserted");
		} else {
			super.getFacade().getSiteLinkService().modifySiteLink(siteLink);
			saveMessage(request, "entity.updated");
		}

		// the line below is added for pagination
		StringBuffer pathBuffer = new StringBuffer();
		pathBuffer.append(mapping.findForward("success").getPath());
		pathBuffer.append("&mod_id=" + mod_id);
		pathBuffer.append("&");
		pathBuffer.append(super.encodeSerializedQueryString(siteLink.getQueryString()));
		ActionForward forward = new ActionForward(pathBuffer.toString(), true);
		// end
		return forward;
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		String id = (String) dynaBean.get("id");
		String mod_id = (String) dynaBean.get("mod_id");
		String[] pks = (String[]) dynaBean.get("pks");

		SiteLink siteLink = new SiteLink();
		siteLink.setMod_id(Long.valueOf(mod_id));
		siteLink.setIs_del(1);
		if (!StringUtils.isBlank(id) && GenericValidator.isLong(id)) {
			siteLink.setId(Long.valueOf(id));
			super.getFacade().getSiteLinkService().removeSiteLink(siteLink);
			saveMessage(request, "entity.deleted");
		} else if (!ArrayUtils.isEmpty(pks)) {
			siteLink.getMap().put("pks", pks);
			super.getFacade().getSiteLinkService().removeSiteLink(siteLink);
			saveMessage(request, "entity.deleted");
		}

		// the line below is added for pagination
		StringBuffer pathBuffer = new StringBuffer();
		pathBuffer.append(mapping.findForward("success").getPath());
		pathBuffer.append("&mod_id=" + mod_id + "&");
		pathBuffer.append(encodeSerializedQueryString(serialize(request, "id", "method", "mod_id")));
		ActionForward forward = new ActionForward(pathBuffer.toString(), true);
		// end
		return forward;
	}
}
